package com.kgc.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.entity.TblUser;

import com.kgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller

public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "user/list")
    @ResponseBody//将方法的返回值转换成json对象
    public Map userList(@RequestParam(name = "page",required = false,defaultValue = "1") int page,
                        @RequestParam(name = "limit",required = false,defaultValue = "10") int limit){
        Map map=new HashMap();
        PageInfo<TblUser> pageInfo=userService.findAll(page,limit);
        map.put("code",0);
        map.put("msg","查询成功!");
        map.put("count",pageInfo.getTotal());//总记录数
        map.put("data",pageInfo.getList());
        return  map;
    }

    //response.setContentType("text/html;charset=UTF-8")
    @RequestMapping(value = "user/save",method = RequestMethod.POST,produces ={"text/html;charset=UTF-8;","application/json;"} )
   @ResponseBody//将方法的返回转换成json对象  {}json对象
    public Map saveUser(TblUser user){
        Map map=new HashMap();
        userService.save(user);
        map.put("msg","增加用户成功");
        map.put("data",user);//id
        return map;
    }

    @RequestMapping("/user/delete/{id}")
    @ResponseBody
    public Map deleteUser(@PathVariable("id")long id){
        Map map=new HashMap();
        userService.delete(id);
        map.put("msg","删除成功!");//{"msg":"删除成功!"}
        return map;
    }



    @RequestMapping("/user/dels")
    @ResponseBody
    public Map delsUser(Long[] ids){
        Map map=new HashMap();
        userService.dels(ids);
        map.put("msg","删除成功!");//{"msg":"删除成功!"}
        return map;
    }
}
